package com.yanqu.road.dao.impl.activity.lottery;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.lottery.ActivityLotteryNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityLotteryNoticeDaoImpl extends TempDataObjectDao<ActivityLotteryNotice> {


    public static String getActivityLotteryNoticeSql = "select * from `t_c_activity_lottery_notice` where `activityId`=? order by `time` desc limit ?";
    public List<ActivityLotteryNotice> getActivityLotteryNotice(int activityId, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ActivityLotteryNotice> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getActivityLotteryNoticeSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                result.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateActivityLotteryNoticeSql = "update `t_c_activity_lottery_notice` set userId=?,userBaseInfo=?,type=?,rewardId=?,time=? where activityId=?";
    @Override
    protected boolean updateData(ActivityLotteryNotice activityLotteryNotice) {
        activityLotteryNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityLotteryNotice.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryNotice.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, activityLotteryNotice.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryNotice.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryNotice.getActivityId()));
        if (getDb().executeNoneQuery(updateActivityLotteryNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertActivityLotteryNoticeSql = "insert into `t_c_activity_lottery_notice`(activityId,userId,userBaseInfo,type,rewardId,time) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(ActivityLotteryNotice activityLotteryNotice) {
        activityLotteryNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryNotice.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(activityLotteryNotice.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryNotice.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, activityLotteryNotice.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryNotice.getTime()));
        if (getDb().executeNoneQuery(insertActivityLotteryNoticeSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ActivityLotteryNotice parseData(ResultSet rs) throws SQLException {
        ActivityLotteryNotice data = new ActivityLotteryNotice();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        data.setType(rs.getInt("type"));
        data.setRewardId(rs.getString("rewardId"));
        data.setTime(rs.getLong("time"));

        return data;
    }
}
